package com.huawei.cloud.user.controller;

import com.huawei.cloud.user.pojo.User;
import com.huawei.cloud.user.service.UserService;
import org.apache.tomcat.util.security.MD5Encoder;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by cWX183898 on 2017/3/16.
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(path = "/user/save", method = RequestMethod.POST)
    public User save(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return userService.save(user);
    }

    @RequestMapping(path = "/user/read_by_username")
    public User readByUsername(String username) {
        User user = userService.readByUsername(username);
        return user;
    }

    @RequestMapping(path = "/user/validate")
    public Map<String, Object> validate(String username, String password) {
        Map<String, Object> result = new HashMap<String, Object>(0);
        StringBuilder error = new StringBuilder();
        boolean validate = userService.validate(username, password, error);
        if (validate) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("error", error);
        }
        return result;
    }

    @RequestMapping(path = "/user/save_with_role", method = RequestMethod.POST)
    public User saveWithRole(User user, String role) {
        return user;
    }
}
